package com.security.exception;


import com.easyCode.feature.result.Result;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 统一访问拒绝响应码为403
 * ExceptionHandlerExceptionResolver查找到容器所有ExceptionWebHandler后会进行排序--->initExceptionHandlerAdviceCache
 * 如果这里不指定高优先级,可能存在被覆盖的情况
 *
 * 当controller方法调用发生异常时,最终会调用ExceptionHandlerExceptionResolver的doResolveHandlerMethodException方法尝试定位异常处理方法
 * @author 大忽悠
 * @create 2023/3/29 20:37
 */
@RestControllerAdvice
@Order(0)
public class SecurityExceptionWebHandler {

    @ResponseStatus(value = HttpStatus.FORBIDDEN)
    @ExceptionHandler({AccessDeniedException.class})
    public Result accessDenyException(AccessDeniedException e) {
        return Result.error(HttpStatus.FORBIDDEN.value(), e.getMessage(), e.getMessage());
    }
}
